package com.jh.gmtv.ui.my;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jh.gmtv.R;
import com.jh.gmtv.bean.ListBean;
import com.jh.gmtv.ui.home.HomeFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {


    private static MyFragment myFragment;
    private ListView mMyLv;
    private List<ListBean> mData;
    private MyListAdapter mAdapter;
    private TextView myBtnLogin;

    public MyFragment() {
        // Required empty public constructor
    }
    //创建Fragment对象
    public  static Fragment setFragment(){
        if (myFragment==null)
            synchronized (HomeFragment.class){
                if (myFragment==null){
                    myFragment = new MyFragment();
                }
            }
        return myFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        //设置数据源
        mData = new ArrayList<>();
        mData.add(new ListBean("房间管理", R.mipmap.ic_profile_host_room, R.mipmap.triangle));
        mData.add(new ListBean("我的关注", R.mipmap.ic_profile_concern, R.mipmap.triangle));
        mData.add(new ListBean("观看历史", R.mipmap.ic_profile_histoy, R.mipmap.triangle));
        mData.add(new ListBean("开播提醒", R.mipmap.icon_profile_remind, R.mipmap.triangle));
        mData.add(new ListBean("种子任务", R.mipmap.ic_profile_task, R.mipmap.triangle));
        mData.add(new ListBean("游戏中心", R.mipmap.ic_profile_game, R.mipmap.triangle));
        //创建适配器
        mAdapter = new MyListAdapter(mData,getContext());
        //设置适配器
        mMyLv.setAdapter(mAdapter);

        //listView点击事件
        mMyLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListBean listBean = (ListBean) parent.getItemAtPosition(position);
                String textName = listBean.getTextName();
                if (textName != null){
                    switch (textName){
                        case "房间管理":
                            Toast.makeText(getContext(), "房间管理", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getActivity(),LoginActivity.class));
                            break;
                        case "我的关注":
                            Toast.makeText(getContext(), "我的关注", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getActivity(),LoginActivity.class));
                            break;
                        case "观看历史":
                            Toast.makeText(getContext(), "观看历史", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getActivity(),LoginActivity.class));
                            break;
                        case "开播提醒":
                            Toast.makeText(getContext(), "开播提醒", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getActivity(),LoginActivity.class));
                            break;
                        case "种子任务":
                            Toast.makeText(getContext(), "种子任务", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getActivity(),LoginActivity.class));
                            break;
                        case "游戏中心":
                            Toast.makeText(getContext(), "游戏中心", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getActivity(),LoginActivity.class));
                            break;
                    }
                }
            }
        });
        //登录按钮
        myBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "登录", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getActivity(),LoginActivity.class));
            }
        });
    }

    private void initView(View view) {
        mMyLv = (ListView) view.findViewById(R.id.my_lv);
        myBtnLogin = (TextView) view.findViewById(R.id.my_btn_login);
    }

}
